import numpy as np
import pandas as pd
# import seaborn as sns
# import matplotlib.pyplot as plt
import altair as alt
!jupyter lab --version
df = pd.read_csv('wz_results_data/wz_results2014.csv')
df.head()
df.shape
df = df[['total_wz_delay', 'avg_wz_delay', 'max_wz_delay', 'max_queue_duration', 'total_queue_duration', 'num_queue', 'max_queue_length',
'wz_id', 'category', 'county', 'road', 'direction', 'speed', 'wz_start', 'wz_end', 'start_mm', 'end_mm', 'tmc_count', 'wz_duration']].dropna()
df['category'] = pd.Categorical(values = df['category'], categories = ['Shoulder Closure', 'Single Lane Closure', 'Double Lane Closure',
'Multiple Lane Closure'])
# total_wz_delay
df2 = df.sort_values(by='total_wz_delay', ascending=False)
df2 = df2.reset_index(drop=True).reset_index()
df2.head()
alt.Chart(df2).mark_point().encode(
x='index',
y='total_wz_delay',
color = 'category',
tooltip=['wz_id', 'category', 'county', 'road', 'direction']
)
base = alt.Chart(df2).mark_circle().encode(
x='index:Q',
y='total_wz_delay:Q',
).properties(
width=1000,
height=300
)
base
chart = base.encode(color = 'category:N',
tooltip=['wz_id', 'category', 'county', 'road', 'direction'],
opacity=alt.value(0.8)).properties(
title='')
chart
chart = chart.encode(
column='category:N'
).properties(
width=250,
height=250
).properties(
title='2014'
)
chart
chart.\
configure_header(
title = None,
titleFontSize=34,
titleFont='Courier',
titleAnchor='middle',
labelColor='blue'
).\
configure_title(
fontSize=34,
anchor='middle',
color='blue')
df = pd.read_csv('wz_results_data/wz_results2014.csv')
df.head()
chart.configure_axis(titleFontSize= 90)
chart.save('chart.png', scale_factor=2.0)
# !conda list
import altair as alt
from vega_datasets import data
source = data.cars.url
chart = alt.Chart(source).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color='Origin:N',
column='Origin:N'
).properties(
width=180,
height=180
)
chart.configure_header(
titleColor='green',
titleFontSize=14,
labelColor='red',
labelFontSize=14
)
chart.save('chart.png', scale_factor=3.0)
chart.save('chart2.svg', scale_factor=2.0)
pd.read_json(data.cars.url).head()
chart = alt.Chart(df2).mark_point().encode(
x='index:Q',
y='total_wz_delay:Q',
color='category:N',
column='category:N'
).properties(
width=180,
height=180
)
chart
df = pd.read_csv('wz_results_data/wz_results_combi.csv')
df = df[['total_wz_delay', 'avg_wz_delay', 'max_wz_delay', 'max_queue_duration', 'total_queue_duration', 'num_queue', 'max_queue_length',
'wz_id', 'category', 'county', 'road', 'direction', 'speed', 'wz_start', 'wz_end', 'start_mm', 'end_mm', 'tmc_count', 'wz_duration', 'lat', 'lon', 'year']].dropna()
df['category'] = pd.Categorical(values = df['category'], categories = ['Shoulder Closure', 'Single Lane Closure', 'Double Lane Closure',
'Multiple Lane Closure'])
# total_wz_delay
df2 = df.sort_values(by='total_wz_delay', ascending=False)
df2 = df2.reset_index(drop=True).reset_index()
df2.head()
base = alt.Chart(df2).mark_circle().encode(
x='index:Q',
y='total_wz_delay:Q',
).properties(
width=1000,
height=300
)
base
chart = base.encode(color = 'category:N',
tooltip=['wz_id', 'category', 'county', 'road', 'direction', 'lat', 'lon', 'year'],
opacity=alt.value(0.8)).properties(
title='')
chart
chart.encode(
column='category:N'
).properties(
width=250,
height=250
)
# ).properties(
# title='2014'
# )
chart_facet = chart.encode(
column='category:N',
row = 'year:N',
).properties(
title='Total Work Zone Delay',
width=250,
height=250
)
chart_facet
chart_facet.\
configure_axisLeft(title=None).\
configure_axisBottom(title=None).\
configure_header(
title = None,
titleFontSize=34,
titleFont='Courier',
titleAnchor='middle',
labelColor='blue',
labelFontSize= 18).\
configure_title(
fontSize=34,
anchor='middle',
color='limegreen').\
interactive()
import altair as alt
from vega_datasets import data
counties = alt.topo_feature(data.us_10m.url, 'counties')
source = data.unemployment.url
alt.Chart(counties).mark_geoshape().encode(
color='rate:Q'
).transform_lookup(
lookup='id',
from_=alt.LookupData(source, 'id', ['rate'])
).project(
type='albersUsa'
).properties(
width=500,
height=300
)
from vega_datasets import data
data.unemployment().sample(5)
counties = alt.topo_feature(data.us_10m.url, 'counties')
counties.url
data.us_10m.description
# pd.read_table(counties.url)
data.us_10m.url
data.us_10m.description
data.us_10m.is_local
data.us_10m()
alt.topo_feature(data.us_10m.url, feature='counties')
alt.topo_feature('https://github.com/deldersveld/topojson/blob/master/countries/us-states/MI-26-michigan-counties.json', feature='states')
data.us_10m.url
data.airports.description
data.airports().head()
url = 'https://github.com/deldersveld/topojson/blob/master/countries/us-states/MI-26-michigan-counties.json'
url = 'https://raw.githubusercontent.com/deldersveld/topojson/master/countries/us-states/MI-26-michigan-counties.json'
url
source = alt.topo_feature(url, feature='cb_2015_michigan_county_20m')
source
background = alt.Chart(source).\
mark_geoshape(
fill='lightgray',
stroke='white').\
encode(tooltip='properties.NAME:N').\
properties(
width=500,
height=500)# .project('albersUsa')#.properties(projection = {'type': 'identity', 'reflectY': True})
background
points = alt.Chart(df2).mark_circle().encode(
longitude='lon:Q',
latitude='lat:Q',
tooltip=['wz_id', 'category', 'county', 'road', 'direction', 'lat', 'lon', 'year'],
color='category:N')
(background + points)
import altair as alt
from vega_datasets import data
airports = data.airports.url
states = alt.topo_feature(data.us_10m.url, feature='states')
# US states background
background = alt.Chart(states).mark_geoshape(
fill='lightgray',
stroke='white'
).properties(
width=500,
height=300
).project('albersUsa')
points = alt.Chart(df2).mark_circle().encode(
longitude='lon:Q',
latitude='lat:Q',
tooltip=['wz_id', 'category', 'county', 'road', 'direction', 'lat', 'lon', 'year'],
color='category:N')
(background + points)
alt.Chart(df2).mark_circle().encode(
longitude='lon:Q',
latitude='lat:Q',
tooltip=['wz_id', 'category', 'county', 'road', 'direction', 'lat', 'lon', 'year'],
color='category:N')#.interactive()
base = alt.Chart(df2).mark_circle().encode(
x='lon:Q',
y='lat:Q',
color='category:N',
tooltip=['wz_id', 'category', 'county', 'road', 'direction', 'lat', 'lon', 'year'],
).interactive()
base
import folium
f = folium.Figure(width=600, height=800)
m = folium.Map(location=[df['lat'].mean(), df['lon'].mean()],
zoom_start=7,
min_lat=df['lat'].min()-0.5,
max_lat=df['lat'].max()+0.5,
min_lon=df['lon'].min()-0.5,
max_lon=df['lon'].max()+0.5,
max_bounds =False)
f.add_child(m)
def add_marker(row):
marker = folium.CircleMarker(location=[row['lat'], row['lon']],
radius=2,
popup= row.wz_id)
marker.add_to(m)
row = df.iloc[0]
df2.iloc[0:500, :].apply(add_marker, axis=1)
m
m
import altair as alt
url = "https://raw.githubusercontent.com/deldersveld/topojson/master/countries/us-states/MI-26-michigan-counties.json"
source = alt.topo_feature(url, feature="cb_2015_michigan_county_20m")
alt.Chart(source).mark_geoshape().encode(
tooltip='properties.NAME:N'
)
nearest = alt.selection(type='single', nearest=True, on='mouseover',
fields=['x'], empty='none')
nearest